﻿using System;
using System.Net;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace Components.ETL
{
    [DataContract]
    public class CitationParserInstruction : InstructionBase
    {
        #region props
        [DataMember]
        public string ConnName { get; set; }
        [DataMember]
        public string SQL { get; set; }
        [DataMember]
        public string KeyField { get; set; }
        [DataMember]
        public string CitationField { get; set; }

        [DataMember]
        public string OutputTable { get; set; }
        [DataMember]
        public string SrcKeyField { get; set; }
        [DataMember]
        public string PubTimeField { get; set; }
        [DataMember]
        public string TitleField { get; set; }
        [DataMember]
        public string BookTitleMeetingField { get; set; }
        [DataMember]
        public string AuthorsField { get; set; }
        [DataMember]
        public string JournalNameField { get; set; }
        [DataMember]
        public string VolumeField { get; set; }
        [DataMember]
        public string IssueField { get; set; }
        [DataMember]
        public string PageField { get; set; }

        [DataMember]
        public CitationParserAlgorithm Algorithm { get; set; }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public CitationParserInstruction()
            : base(Guid.Empty, Guid.Empty)
        {
            this.ConnName = string.Empty;
            this.SQL = string.Empty;
            this.KeyField = string.Empty;
            this.CitationField = string.Empty;

            this.OutputTable = string.Empty;
            this.SrcKeyField = string.Empty;
            this.PubTimeField = string.Empty;
            this.TitleField = string.Empty;
            this.BookTitleMeetingField = string.Empty;
            this.AuthorsField = string.Empty;
            this.JournalNameField = string.Empty;
            this.VolumeField = string.Empty;
            this.IssueField = string.Empty;
            this.PageField = string.Empty;
            this.Algorithm = CitationParserAlgorithm.DecisionTree;
        }

        /// <summary>
        /// constructor, all derived class must call default constructor to initialize
        /// default settings.
        /// </summary>
        public CitationParserInstruction(Guid componentID, Guid jobTicketID,
            string connName, string sql,
            string keyField, string citeTextField,
            string outputTable, string srcKeyField, string pubTimeField,
            string titleField, string bookTitleMeeting, string authorsField,
            string journalField, string volField,
            string issueField, string pageField,
            CitationParserAlgorithm algorithm)
            : base(componentID, jobTicketID)
        {
            this.ConnName = connName;
            this.SQL = sql;
            this.KeyField = keyField;
            this.CitationField = citeTextField;

            this.OutputTable = outputTable;
            this.SrcKeyField = srcKeyField;
            this.PubTimeField = pubTimeField;
            this.TitleField = titleField;
            this.BookTitleMeetingField = bookTitleMeeting;
            this.AuthorsField = authorsField;
            this.JournalNameField = journalField;
            this.VolumeField = volField;
            this.IssueField = issueField;
            this.PageField = pageField;
            this.Algorithm = algorithm;
        }
        #endregion

        #region Overrides of InstructionBase

        public override XElement Serialize()
        {
            XElement xe = base.Serialize();
            xe.Add(new XAttribute("ConnName", this.ConnName));
            xe.Add(new XAttribute("SQL", this.SQL));
            xe.Add(new XAttribute("KeyField", this.KeyField));
            xe.Add(new XAttribute("CitationField", this.CitationField));

            xe.Add(new XAttribute("OutputTable", this.OutputTable));
            xe.Add(new XAttribute("SrcKeyField", this.SrcKeyField));
            xe.Add(new XAttribute("PubTimeField", this.PubTimeField));
            xe.Add(new XAttribute("TitleField", this.TitleField));
            xe.Add(new XAttribute("BookTitleMeetingField", this.BookTitleMeetingField));
            xe.Add(new XAttribute("AuthorsField", this.AuthorsField));
            xe.Add(new XAttribute("JournalNameField", this.JournalNameField));
            xe.Add(new XAttribute("VolumeField", this.VolumeField));
            xe.Add(new XAttribute("IssueField", this.IssueField));
            xe.Add(new XAttribute("PageField", this.PageField));
            xe.Add(new XAttribute("Algorithm", this.Algorithm.ToString()));

            return xe;
        }

        public override InstructionBase Instantiate(XElement xe)
        {
            CitationParserInstruction instruction = (CitationParserInstruction)base.Instantiate(xe);

            instruction.ConnName = xe.Attribute("ConnName").Value;
            instruction.SQL = xe.Attribute("SQL").Value;
            instruction.KeyField = xe.Attribute("KeyField").Value;
            instruction.CitationField = xe.Attribute("CitationField").Value;

            instruction.OutputTable = xe.Attribute("OutputTable").Value;
            instruction.SrcKeyField = xe.Attribute("SrcKeyField").Value;
            instruction.PubTimeField = xe.Attribute("PubTimeField").Value;
            instruction.TitleField = xe.Attribute("TitleField").Value;
            instruction.BookTitleMeetingField = xe.Attribute("BookTitleMeetingField").Value;
            instruction.AuthorsField = xe.Attribute("AuthorsField").Value;
            instruction.JournalNameField = xe.Attribute("JournalNameField").Value;
            instruction.VolumeField = xe.Attribute("VolumeField").Value;
            instruction.IssueField = xe.Attribute("IssueField").Value;
            instruction.PageField = xe.Attribute("PageField").Value;
            instruction.Algorithm =
                (CitationParserAlgorithm)Enum.Parse(typeof(CitationParserAlgorithm), xe.Attribute("Algorithm").Value, false);

            return instruction;
        }

        #endregion
    }
}
